package com.ayu.wxgzh.ayucyy.chain.handler;

import com.ayu.wxgzh.ayucyy.entity.Article;
import com.ayu.wxgzh.ayucyy.entity.BaseMessage;
import com.ayu.wxgzh.ayucyy.service.VodService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Map;
import java.util.Objects;

/**
 * @Author 徐林玉
 * @Description VideoSearchHandler
 * @Date 2022/9/2 10:42
 * @Version 1.0
 */
@Component
@Slf4j
public class VideoSearchHandler extends AbstractHandler {

    private final VodService vodService;

    @Autowired
    public VideoSearchHandler(VodService vodService) {
        this.vodService = vodService;
    }

    @Override
    public BaseMessage handle(Map<String, String> requestMap) {

        //降级：如果配置了降级，则跳过此处理器，执行下一个处理器
        if (super.getConfig().getDown()) {
            log.warn("电影查询 VideoSearchHandler 已降级，跳过电影查询 VideoSearchHandler...");
            return super.next(requestMap);
        }
        String content = requestMap.get("Content");
        Article articleByText = vodService.getArticleByText(content);

        BaseMessage articleMessage = getArticleMessage(articleByText, requestMap);

        return Objects.nonNull(articleMessage) ? articleMessage : super.next(requestMap);
    }
}
